/*package com.youoor.mvc;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.TypeMismatchException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.youoor.common.constant.AccountConst;
import com.youoor.common.constant.SysConst;
import com.youoor.common.utils.HttpClientUtil;
import com.youoor.common.utils.JSONUtils;
import com.youoor.common.utils.PropertiesUtil;
import com.youoor.domain.Account;
import com.youoor.domain.AccountQuery;
import com.youoor.dto.RetDTO;
import com.youoor.enums.ErrorCodeEnum;
import com.youoor.exception.ServiceException;
import com.youoor.service.AccountService;

@Controller
@RequestMapping(value = { "/open", "/core", "/inner" }, produces = "application/json")
public class AccountController extends AbstractController {
	private static final Logger logger = LoggerFactory.getLogger(AccountController.class);
	private static final int ERROR_STATUS = 599;
	
	@Resource(name = "accountService")
	private AccountService accountService;

	public void setAccountService(AccountService accountService) {
		this.accountService = accountService;
	}

	static String token = "null";

	@RequestMapping("/txCallbackToken")
	public ModelAndView txCallbackToken(
			@RequestParam("code") String code, 
			@RequestParam("openid") String openId, 
			@RequestParam("openkey") String openKey) {
		String tokenJson = null;
		try {
			Map<String,String> postParam = new HashMap<String,String>(5);
			postParam.put("client_id", PropertiesUtil.getProperty("tx_app_key", SysConst.SYS_PROPERTIES_FILE));
			postParam.put("client_secret", PropertiesUtil.getProperty("tx_app_secret", SysConst.SYS_PROPERTIES_FILE));
			postParam.put("grant_type", PropertiesUtil.getProperty("tx_grant_type", SysConst.SYS_PROPERTIES_FILE));
			postParam.put("redirect_uri", PropertiesUtil.getProperty("tx_redirect_url", SysConst.SYS_PROPERTIES_FILE));
			postParam.put("code", code);
			
			tokenJson = HttpClientUtil.getInstance().executePost(PropertiesUtil.getProperty("tx_access_token_url", SysConst.SYS_PROPERTIES_FILE), postParam);
			token += ";+++++++++++++" + tokenJson;
			Map<String, String> tempt = new HashMap<String,String>();
			for(String p : tokenJson.split("&")){
				if(p.split("=").length == 2)
					tempt.put(p.split("=")[0], p.split("=")[1]);
			}
			
			AccountQuery aq = new AccountQuery();
			aq.createCriteria().andPlatformTypeEqualTo(AccountConst.ACCOUNT_PLATFORM_TYPE_TX).andAccountIdEqualTo(String.valueOf(tempt.get("name")));
			List<Account> as = accountService.selectByExample(aq);
			Account a = null;
			if(as.size() > 0){
				if(as.size() != 1){
					throw new Exception("系统错误，账号："+String.valueOf(tempt.get("name")) +"在系统中重复出现。");
				}
				a = as.get(0);
				if(1 != a.getUserId()){//账号已经被另一个账号绑定
					token = "账号已经被另一个账号绑定";
					ModelAndView result = new ModelAndView();
					result.setViewName("/close.jsp");
					return result;
				}
				a.setModifyTime(new Date());
			}else{
				a = new Account();
				a.setUserId(1);
				a.setPlatformType(AccountConst.ACCOUNT_PLATFORM_TYPE_TX);
			}
			
			a.setAccessToken(String.valueOf(tempt.get("access_token")));
			a.setExpiresIn(Long.parseLong((String.valueOf(tempt.get("expires_in")))));
			a.setRefreshToken(String.valueOf(tempt.get("refresh_token")));
			a.setAccountId(String.valueOf(tempt.get("name")));
			a.setAccountName(String.valueOf(tempt.get("nick")));
			
			if(a.getId() == null){
				accountService.save(a);
			}else{
				accountService.updateByPrimaryKeySelective(a);
			}

		} catch (IOException e) {
			e.printStackTrace();
		} catch (Exception e) {
			logger.error(tokenJson);
			e.printStackTrace();
		} 
		ModelAndView result = new ModelAndView();
		result.setViewName("/close.jsp");
		return result;
	}

	@RequestMapping("/sinaCallbackToken")
	public ModelAndView sinaCallbackToken(@RequestParam("code") String code) {

		try {
			Map<String,String> postParam = new HashMap<String,String>(5);
			postParam.put("client_id", PropertiesUtil.getProperty("sina_app_key", SysConst.SYS_PROPERTIES_FILE));
			postParam.put("client_secret", PropertiesUtil.getProperty("sina_app_secret", SysConst.SYS_PROPERTIES_FILE));
			postParam.put("grant_type", PropertiesUtil.getProperty("sina_grant_type", SysConst.SYS_PROPERTIES_FILE));
			postParam.put("redirect_uri", PropertiesUtil.getProperty("sina_redirect_url", SysConst.SYS_PROPERTIES_FILE));
			postParam.put("code", code);
			
			String tokenJson = HttpClientUtil.getInstance().executePost(PropertiesUtil.getProperty("sina_access_token_url", SysConst.SYS_PROPERTIES_FILE), postParam);
			token += ";+++++++++++++" + tokenJson;
			Map<String,Object> tokenMap = JSONUtils.json2map(tokenJson);
			
			//获取新浪账号用户信息
			postParam.clear();
			postParam.put("access_token", tokenMap.get("access_token").toString());
			postParam.put("uid", tokenMap.get("uid").toString());
			
			String userJson = HttpClientUtil.getInstance().executeGet(PropertiesUtil.getProperty("sina_user_info_url", SysConst.SYS_PROPERTIES_FILE), postParam);
			token += ";+++++++++++++" + userJson;
			Map<String,Object> userMap = JSONUtils.json2pojo(userJson, Map.class);
			
			AccountQuery aq = new AccountQuery();
			aq.createCriteria().andPlatformTypeEqualTo(AccountConst.ACCOUNT_PLATFORM_TYPE_SINA).andAccountIdEqualTo(String.valueOf(tokenMap.get("uid")));
			List<Account> as = accountService.selectByExample(aq);
			Account a = null;
			if(as.size() > 0){
				if(as.size() != 1){
					throw new Exception("系统错误，账号："+String.valueOf(tokenMap.get("uid")) +"在系统中重复出现。");
				}
				a = as.get(0);
				if(1 != a.getUserId()){//账号已经被另一个账号绑定
					token = "账号已经被另一个账号绑定";
					ModelAndView result = new ModelAndView();
					result.setViewName("/close.jsp");
					return result;
				}
				a.setModifyTime(new Date());
			}else{
				a = new Account();
				a.setUserId(1);
				a.setPlatformType(AccountConst.ACCOUNT_PLATFORM_TYPE_SINA);
			}
			
			
			a.setAccessToken(String.valueOf(tokenMap.get("access_token")));
			a.setExpiresIn(Long.parseLong((String.valueOf(tokenMap.get("expires_in")))));
			a.setAccountId(String.valueOf(tokenMap.get("uid")));
			a.setAccountName(String.valueOf(userMap.get("screen_name")));
			
			if(a.getId() == null){
				accountService.save(a);
			}else{
				accountService.updateByPrimaryKeySelective(a);
			}
		} catch (IOException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		} 

		ModelAndView result = new ModelAndView();
		result.setViewName("/close.jsp");
		return result;
	}

	@RequestMapping("/showToken")
	public RetDTO showToken() {
		RetDTO retDTO = new RetDTO();
		retDTO.addResult("result", token);
		retDTO.markSuccess();
		return retDTO;
	}


}
*/